/**
* 版权所有：福建邮科电信业务部厦门研发中心 
*====================================================
* 文件名称: MyStruts2Filter.java
* 修订记录：
* No    日期				作者(操作:具体内容)
* 1.    Jun 12, 2012			詹保山(softzbs@126.com)(创建:创建文件)
*====================================================
* 类描述：(说明未实现或其它不应生成javadoc的内容)
* 
*/
package com.devframe.web.struts2.dispatcher;

import java.io.IOException;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.dispatcher.ng.ExecuteOperations;
import org.apache.struts2.dispatcher.ng.InitOperations;
import org.apache.struts2.dispatcher.ng.filter.FilterHostConfig;


/**
 *<pre><b><font color="blue">MyStruts2Filter</font></b></pre>
 *
 *<pre><b>&nbsp;--描述说明--</b></pre>
 * <pre></pre>
 * <pre>
 * <b>--样例--</b>
 *   MyStruts2Filter obj = new MyStruts2Filter();
 *   obj.method();
 * </pre>
 */
public class MyStruts2ActionFilter  implements StrutsStatics, Filter {
	  private static final Logger LOG = Logger.getLogger(MyStruts2ActionFilter.class);
	  protected MyPrepareOperations prepare;
	    protected ExecuteOperations execute;
		protected List<Pattern> excludedPatterns = null;

	    public void init(FilterConfig filterConfig) throws ServletException {
	        InitOperations init = new InitOperations();
	        try {
	            FilterHostConfig config = new FilterHostConfig(filterConfig);
	            init.initLogging(config);
	            Dispatcher dispatcher = init.initDispatcher(config);
	            init.initStaticContentLoader(config, dispatcher);
	            prepare = new MyPrepareOperations(filterConfig.getServletContext(), dispatcher);
	            execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
				this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);
	            postInit(dispatcher, filterConfig);
	        } finally {
	            init.cleanup();
	        }
	        

	    }

	    /**
	     * Callback for post initialization
	     */
	    protected void postInit(Dispatcher dispatcher, FilterConfig filterConfig) {
	    }
	    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
	        HttpServletRequest request = (HttpServletRequest) req;
	        HttpServletResponse response = (HttpServletResponse) res;
	        String method=request.getParameter("method");
	        if(StringUtils.isEmpty(method)){
		        try {
		            prepare.setEncodingAndLocale(request, response);
		            prepare.createActionContext(request, response);
		            prepare.assignDispatcherToThread();
					if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
						chain.doFilter(request, response);
					} else {
						request = prepare.wrapRequest(request);
						ActionMapping mapping = prepare.findActionMapping(request, response, true);
						if (mapping == null) {
							boolean handled = execute.executeStaticResourceRequest(request, response);
							if (!handled) {
								chain.doFilter(request, response);
							}
						} else {
							//执行ACTION的操作
							if(prepare.containsAction(mapping)){
							  execute.executeAction(request, response, mapping);
							}else{
								if(LOG.isDebugEnabled()){
								 LOG.debug("Sruts2t不包含该ACTION "+mapping.getName()+"，继续运行");
								}
							    chain.doFilter(request, response);
							}
						}
					}
		        } finally {
		            prepare.cleanupRequest(request);
		        }
	        }else{
				if(LOG.isDebugEnabled()){
					 LOG.debug("包含method参数");
					}
	        	  chain.doFilter(request, response);
	        }
	    }

	    public void destroy() {
	        prepare.cleanupDispatcher();
	    }
}
